/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package it.txt.access.capability.commons.schema.factory;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.xml.sax.InputSource;
import static it.txt.access.capability.commons.schema.CapabilitySchemaLocations.*;
import static it.txt.access.capability.commons.schema.CapabilityNamespacesMapper.*;

/**
 *
 * @author seccia
 */
public class CapabilitySchemasLoader {

    private static final Logger LOGGER = Logger.getLogger(CapabilitySchemasLoader.class.getName());

    protected Set<CapabilitySchemaSource> capabilitySchemaSources;
    
    public static final String IMPORTED_SCHEMAS_PATH =
            File.separator +
            ACCESS_CAPABILITY_IMPORT_FOLDER +
            File.separator;

    public static final String CAPABILITY_SCHEMA_PATH =
            File.separator +
            ACCESS_CAPABILITY_FOLDER +
            File.separator;
    
    
    public CapabilitySchemasLoader(){
        capabilitySchemaSources = new HashSet<CapabilitySchemaSource>();
    }

    public Set<CapabilitySchemaSource> loadCapabilityEntities(String capabilitySchemasRootPath) {
        loadBasicCapabilityEntities(capabilitySchemasRootPath);
        return capabilitySchemaSources;
    }

    public Set<CapabilitySchemaSource> loadDemoCapabilityEntities(String capabilitySchemasRootPath) {
        loadBasicCapabilityEntities(capabilitySchemasRootPath);
        registerSchema(CAPABILITY_DEMO_REQUEST_QNAME.getNamespaceURI(),
                capabilitySchemasRootPath + CAPABILITY_SCHEMA_PATH
                + ACCESS_CAPABILITY_FILE);
        return capabilitySchemaSources;
    }

    protected void loadBasicCapabilityEntities(String capabilitySchemasRootPath) {

        registerSchema(IMPORTED_SCHEMA_XS_NS,
                capabilitySchemasRootPath + IMPORTED_SCHEMAS_PATH
                + ACCESS_CAPABILITY_XS_FILE);

        registerSchema(IMPORTED_SCHEMA_DS_NS,
                capabilitySchemasRootPath + IMPORTED_SCHEMAS_PATH
                + ACCESS_CAPABILITY_DS_FILE);

        registerSchema(IMPORTED_SCHEMA_XENC_NS,
                capabilitySchemasRootPath + IMPORTED_SCHEMAS_PATH
                + ACCESS_CAPABILITY_XENC_FILE);

        registerSchema(IMPORTED_SCHEMA_XACML_QNAME_NS,
                capabilitySchemasRootPath + IMPORTED_SCHEMAS_PATH
                + ACCESS_CAPABILITY_XACML_FILE);

        registerSchema(IMPORTED_SCHEMA_SAML_NS,
                capabilitySchemasRootPath + IMPORTED_SCHEMAS_PATH
                + ACCESS_CAPABILITY_SAML_FILE);

        registerSchema(ACCESS_RIGHTS_CAPABILITY_QNAME.getNamespaceURI(),
                capabilitySchemasRootPath + CAPABILITY_SCHEMA_PATH
                + ACCESS_CAPABILITY_FILE);
    }

    protected void registerSchema(String namespace, String fileName) {
        LOGGER.log(Level.INFO, "Registering [{0}] with file name [{1}]",
                new Object[]{namespace, fileName});
        try {
            InputStream inputStream = new FileInputStream(fileName);
            InputSource xsdSource = new InputSource(inputStream);
            CapabilitySchemaSource capabilitySource =
                    new CapabilitySchemaSource(xsdSource);
            capabilitySource.setTargetNamespace(namespace);
            File file = new File(fileName);
            capabilitySource.setTargetURI(file.toURI());
            capabilitySchemaSources.add(capabilitySource);
        }
        catch (Exception ex) {
            LOGGER.log(Level.SEVERE, ex.getMessage(), ex);
        }
    }
}
